﻿using System;
using System.Windows;
using System.Collections.Generic;
using XAMLStylePad.ViewModels.TriggersAndSetters;
using FireFly;
using System.Collections;

namespace XAMLStylePad.ViewModels
{
    public class StyleViewModel : XamlViewModelBase<Style>
    {
        #region .ctor
        public StyleViewModel(Style style): base(style)
        {
        }
        #endregion

        #region BasedOn
        public StyleViewModel BasedOn
        {
            get
            {
                return _BasedOn;
            }
            set
            {
                _BasedOn = value;
                Model.BasedOn = value.Model; 
                OnPropertyChanged("BasedOn");
            }
        }
        StyleViewModel _BasedOn = null;
        #endregion

        #region TargetType
        public Type TargetType
        {
            get
            {
                return Model.TargetType;
            }
            set
            {
                Model.TargetType = value;
                OnPropertyChanged("TargetType");
            }
        }
        #endregion

        public object ResourceKey
        {
            get;
            set;
        }

        #region Triggers
        public CollectionViewModel<TriggerBase, ITriggerViewModel> Triggers
        {
            get
            {
                if(_Triggers == null)
                {
                    _Triggers = new CollectionViewModel<TriggerBase, ITriggerViewModel>(Model.Triggers, 
                        trigger =>
                    {
                        if(trigger is Trigger)
                            return new TriggerViewModel(trigger as Trigger);
                        else if(trigger is DataTrigger)
                            return new DataTriggerViewModel(trigger as DataTrigger);
                        else if(trigger is MultiTrigger)
                            return new MultiTriggerViewModel(trigger as MultiTrigger);
                        else if(trigger is MultiDataTrigger)
                            return new MultiDataTriggerViewModel(trigger as MultiDataTrigger);
                        else if(trigger is EventTrigger)
                            return new EventTriggerViewModel(trigger as EventTrigger);
                        else
                            return null;
                    })
                    {
                        DisplayName = "Triggers",
                        Parent = this
                    };
                }
                return _Triggers;
            }
        }
        CollectionViewModel<TriggerBase, ITriggerViewModel> _Triggers;
        #endregion

        #region Setters
        public SetterCollectionViewModel Setters
        {
            get
            {
                if(_Setters == null)
                {
                    _Setters = new SetterCollectionViewModel(Model.Setters)
                    {
                        DisplayName = "Setters",
                        Parent = this
                    };
                }
                return _Setters;
            }
        }
        SetterCollectionViewModel _Setters;
        #endregion

        #region overrides
        public override string DisplayName
        {
            get
            {
                return string.Format("{0} [{1}]{2}",
                    ResourceKey is Type ? "Style for " + ((Type)ResourceKey).Name : ResourceKey,
                    TargetType != null ? TargetType.Name : "<empty>",
                    BasedOn != null ? ": " + (BasedOn.ResourceKey != null ? BasedOn.ResourceKey : "<no key>").ToString() : "");
            }
        }
        
        public override ICollection VisualChildren
        {
            get
            {
                return ViewModelService.Instance.CombineVisualChildren(Setters, Triggers);
            }
        }
        #endregion
    }
}
